<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Cart;

class CartController extends Controller
{
    use ModelForm;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('购物车');
            $content->description('管理');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('购物车');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('购物车');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Cart::class, function (Grid $grid) {
            // $grid->position_tags('轮播图序号');
            $grid->id('序号');
            $grid->commodity_name('商品名称');
            $grid->commodity_id('商品id');
            $grid->img('商品图片')->image();
            $grid->price('商品价格');
            $grid->user_id('用户id');
            $grid->commodity_series_id('商品型号id');
            $grid->num('商品数量');
            $grid->receiving_address_id('收货地址id');
            $grid->leaving_a_message('留言');
            $grid->disableFilter();
            $grid->disableExport();
            $is_selected = [
            'on' => ['value' => '0', 'text' => '取消', 'color' => 'success'],
            'off' => ['value' => '1', 'text' => '选中', 'color' => 'danger'],
            ];
            $grid->column('selected','选中')->switch($is_selected);
            // $grid->disableCreateButton();
            // $grid->disablePagination();
            // $grid->disableFilter();
            $grid->paginate(10);
            $grid->perPages([10,20,30]);
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            // $grid->disableActions();
            $grid->actions(function ($actions) {
                // $actions->disableDelete();
                $actions->disableEdit();
            });
        });
    }

    protected function form()
    {
        return Admin::form(Cart::class, function (Form $form) {
            $form->text('commodity_name', '商品名称');
            $form->text('price', '商品价格');
            $form->text('commodity_id', '商品id');
            $form->text('user_id', '用户id');
            $form->text('commodity_series_id', '商品型号id');
            $form->text('num', '商品数量');
            $form->text('receiving_address_id', '收货地址id');
            $form->text('leaving_a_message', '留言');
            $form->image('img', '商品图片');
            $states = [
 'on'  => ['value' => 1, 'text' => '选中', 'color' => 'success'],
 'off' => ['value' => 0, 'text' => '取消', 'color' => 'danger'],
];
            $form->switch("selected","开启热销")->states($states);
        });
    }
    }
